package ServerStuff;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.sql.Date;
import java.util.Calendar;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import Utilities.PayrollCalculator;

import DataStorageClasses.PayrollSheet;
import Database.DatabaseConnector;

@WebServlet("/CalculatePayrollServlet")
public class CalculatePayrollServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
	{
		try {
			PayrollCalculator prCalc;
			String business = (String) request.getSession().getAttribute("username");
			Long employeeID = null;
			try {
				employeeID = Long.parseLong(request.getParameter("empID"));
			} catch(NullPointerException e) {
				request.setAttribute("failed", true);
				request.setAttribute("message", "Must Include an Employee ID");
			} catch (NumberFormatException e) {
				request.setAttribute("failed", true);
				request.setAttribute("message", "Employee ID must be a number");
			}
			
			if (employeeID != null) {
				try {
					Calendar calendar = Calendar.getInstance();
					String[] date = request.getParameter("startDate").split("-");
					calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1])-1, Integer.parseInt(date[2]));
					Date start = new Date(calendar.getTimeInMillis());
					
					date = request.getParameter("endDate").split("-");
					calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1])-1, Integer.parseInt(date[2]));
					Date finish = new Date(calendar.getTimeInMillis());
					
					prCalc = new PayrollCalculator(business, employeeID, 0.13, start, finish); 
					PayrollSheet prSheet = prCalc.getPayroll();
					
					if (prSheet != null) {
						request.setAttribute("failed", false);
						response.sendRedirect("/GoGoGadget/ShowPayroll.jsp?empID=" + prSheet.getID()
																   + "&firstName=" + prSheet.getFirstName()
																   + "&lastName=" + prSheet.getLastName()
																   + "&wage=" + prSheet.getWage()
																   + "&totalHours=" + prSheet.getTotalHours()
																   + "&ot=" + prSheet.getOverTimeHours()
																   + "&start=" + prSheet.getStartDate()
																   + "&end=" + prSheet.getEnd()
																   + "&tax=" + prSheet.getTax()
																   + "&preTax=" + prSheet.getTotalBeforeTax()
																   + "&postTax=" + prSheet.getTotalAfterTax()
																   + "&taxDeduct=" + prSheet.getTaxDeduction()
																   + "&business=" + business);
						
						response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
						return;
					}
					
					else {
						request.setAttribute("failed", true);
						request.setAttribute("message", "Unable to create sheet, employee id might not be valid");
					}
				} catch(NullPointerException e) {
					request.setAttribute("failed", true);
					request.setAttribute("message", "Invalid Dates");
				} catch (NumberFormatException e) {
					request.setAttribute("failed", true);
					request.setAttribute("message", "Invalid Dates");
				}
			}
			
			RequestDispatcher dispatcher = request.getRequestDispatcher("/PayrollPage.jsp");
			dispatcher.forward(request, response);
			response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
		}
		catch(NullPointerException e) {
			e.printStackTrace();
			response.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
